<?php

/**
 * Louder Cache
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file docs/LICENSE.txt.
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to info@loudertechnology.com so we can send you a copy immediately.
 *
 * @category Louder
 * @package NodeCommunicator
 * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
 * @license New BSD License
 */

/**
 * Implementa la comunicacion con el nodo
 *
 * @category Louder
 * @package NodeCommunicator
 * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
 * @license New BSD License
 */
class NodeCommunicator {

	/**
	 * Adaptador de Conexion HTTP
	 *
	 * @var unknown_type
	 */
	private $_adapter;

	/**
	 * Headers a ser enviados en la peticion
	 *
	 * @var array
	 */
	private $_headers = array();

	/**
	 * Constructor de la clase
	 *
	 * @access public
	 * @param string $scheme
	 * @param string $address
	 * @param string $uri
	 * @param string $method
	 */
	public function __construct($scheme, $address, $uri){
		$method = $this->_getRequestMethod();
		if(!class_exists("SocketsCommunicator")){
			require "Library/Louder/NodeCommunicator/CommunicatorInterface.php";
			require "Library/Louder/NodeCommunicator/Adapters/Sockets.php";
			require "Library/Louder/NodeCommunicator/Adapters/PeclHTTP.php";
		}
		#$this->_adapter = new SocketsCommunicator($scheme, $address, $uri, $method);
		$this->_adapter = new PeclHTTPCommunicator($scheme, $address, $uri, $method);
	}

	/**
	 * Obtener el metodo con el que se realiza la peticion
	 *
	 * @access private
	 * @return string
	 */
	private function _getRequestMethod(){
		return $_SERVER['REQUEST_METHOD'];
	}

	/**
	 * Establece el Session Cluster ID para la peticion
	 *
	 * @param string $sessIDName
	 * @param string $sessIDValue
	 */
	public function setSessionClusterId($sessIDName, $sessIDValue){
		$this->_headers[$sessIDName] = $sessIDValue;
	}

	/**
	 * Agrega los parametros por GET a la peticion
	 *
	 * @access public
	 */
	public function prepareQueryData(){
		$this->_adapter->prepareQueryData($_GET);
	}

	/**
	 * Reemplaza los headers de la peticion por los realizados al cluster
	 *
	 * @access public
	 */
	public function prepareHeaders(){
		$this->_adapter->setHeaders(array(
			'Host' => $_SERVER['HTTP_HOST'],
			'Connection' => $_SERVER['HTTP_CONNECTION'],
			'Accept' => $_SERVER['HTTP_ACCEPT'],
			'Accept-Encoding' => $_SERVER['HTTP_ACCEPT_ENCODING'],
			'User-Agent' => $_SERVER['HTTP_USER_AGENT']
		) + $this->_headers);
	}

	/**
	 * Envia la peticion HTTP
	 *
	 * @access public
	 */
	public function sendRequest(){
		$this->_adapter->send();
	}

	/**
	 * Indica si el resultado obtenido tiene determinado Content-Type
	 *
	 * @param string $contentType
	 * @return boolean
	 */
	public function isResponseContentType($contentType){
		$headers = $this->_adapter->getResponseHeaders();
		if(isset($headers['Content-Type'])){
			if(is_array($contentType)){
				$contentTypeList = $contentType;
				foreach($contentTypeList as $contentType){
					if(strpos($headers['Content-Type'], $contentType)!==false){
						return true;
					}
				}
				return false;
			} else {
				if(strpos($headers['Content-Type'], $contentType)!==false){
					return true;
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	}

	/**
	 * Devuelve los headers recibidos de la peticion
	 *
	 * @return array
	 */
	public function getResponseHeaders(){
		return $this->_adapter->getResponseHeaders();
	}

	/**
	 * Devuelve el cuerpo de la respuesta HTTP
	 *
	 * @return string
	 */
	public function getResponseBody(){
		return $this->_adapter->getResponseBody();
	}

	/**
	 * Indica si el resultado de la peticion fue exitoso
	 *
	 * @return boolean
	 */
	public function isResponseStatusOK(){
		if($this->_adapter->getResponseCode()==200){
			return true;
		} else {
			return false;
		}
	}

}
